<template>
		<el-form style="margin-top:50px;height:100%" ref="form" :model="sizeForm" label-width="180px" size="mini">
      <el-form-item label="平台授权信息" style="font-weight:900"></el-form-item>
			<el-form-item label="序列号:" v-model="sizeForm.sn">
				<span>{{ sizeForm.sn }}</span>
			</el-form-item>
			<el-form-item label="激活码:" v-model="sizeForm.data.$activationCode">
				<span>{{ sizeForm.data.$activationCode }}</span>
			</el-form-item>
			<el-form-item
				label="取号机数量:"
				v-model="sizeForm.data.$mips_takerAmount"
			>
				<span>{{ sizeForm.data.$mips_takerAmount }}</span>
			</el-form-item>
			<el-form-item
				label="虚拟叫号器数量:"
				v-model="sizeForm.data.$mips_callerAmount"
			>
				<span>{{ sizeForm.data.$mips_callerAmount }}</span>
			</el-form-item>
			<el-form-item label="证书信息:" v-model="sizeForm.content">
				<el-input
					type="textarea"
					style="width:40%"
          autosize
					v-model="sizeForm.content"
					disabled
				>
					{{ sizeForm.content }}
				</el-input>
			</el-form-item>
			<el-form-item label="授权验证:" v-model="sizeForm.context.passed">
				<span>{{sizeForm.context.verification }}</span>
			</el-form-item>
			<el-form-item size="large">
				<el-button type="primary" @click="backup">备份</el-button>
				<el-upload
					class="importFile"
					:show-file-list="false"
					:action="`${VUE_APP_BASE_API}/system/license/import`"
					:on-success="submitSuccess"
				>
					<el-button size="large" type="primary">导入</el-button>
				</el-upload>
			</el-form-item>
		</el-form-item>
    </el-form>
</template>
<script>
import {
	getAuthInfo,
	backupAuthInfo,
	importAuthInfo,
} from '@/api/system/authInfo'
export default {
	data() {
		return {
			VUE_APP_BASE_API: process.env.VUE_APP_BASE_API,
			sizeForm: {
				sn: '',
				filename: '',
				content: '',
				verification: '',
				data: {
					//授权激活码
					$activationCode: '',
					//授权码版本
					$version: '',
					//授权码发放时间戳（ISO8601日期时间字符串
					$releasedAt: '',
					//：授权生效起始时间戳（ISO8601日期时间字符串）
					$validBeginAt: '',
					//授权生效终止时间戳（ISO8601日期时间字符串）
					$validEndAt: '',
					//授权有效时长（毫秒）
					$validDuration: '',
					//预警时间（天）
					$preAlarmDays: '',
					//：预警类别（1~5，默认1）
					$preAlarmLevel: '',
					//：宽限期长（天）
					$extendDays: '',
					//：宽限期告警类别（1~5，默认2）
					$extendAlarmLevel: '',
					//最终通告告警类别（1~5，默认3）
					$finalAlarmLevel: '',
					//允许接入的取号器数量
					// mipsTakerAmount: '',
					// mipsCallerAmount: '',
          $mips_callerAmount:'',
          $mips_takerAmount:''

				},
				context: {
					//当前授权是否通过（false=授权失败；true=授权成功）
					passed: '',
					//当前所处限制等级
					level: '',
					//当前已接入的取号器数量
					mipsTakerAmount: '',
					//当前已接入的虚拟叫号器数量
					mipsCallerAmount: '',
					// 有效期
					phase: '',
					// 授权状态
					verification: '',
				},
			},
		}
	},
	async created() {
		const { data } = await getAuthInfo()
		this.sizeForm = data
		

		let diffTime = Date.now() - this.sizeForm.data.$validEndAt
		// 宽限期内
		if (
			diffTime < 0 &&
			-diffTime <= this.sizeForm.data.$extendDays * 86400000
		) {
      // setInterval(() => {
		// 	this.$notify({
		// 		title: '授权过期',
		// 		message: '系统授权已过期，所有功能已停止，请及时联系厂家或代理商代理',
		// 		position: 'bottom-left',
		// 	})
		// }, 30*60 * 1000)
			// 毫秒（预警期内）
		} else if (this.sizeForm.data.$preAlarmDays * 86400000 > diffTime >= 0) {
      
		}
	},
	computed: {},
	methods: {
		// 备份(调用接口即可)
		async backup() {
			try {
			const data = 	await backupAuthInfo(this.sizeForm.filename)
     let url = `${process.env.VUE_APP_BASE_API}/system/license/backup?filename=${this.sizeForm.filename}`
      　const anchor = document.createElement('a');
       anchor.href = url;

       anchor.setAttribute("download", this.sizeForm.filename);
        anchor.className = "download-js-link";
        anchor.innerHTML = "downloading...";
        anchor.style.display = "none";
        document.body.appendChild(anchor);
        setTimeout(function () {
          anchor.click();
          document.body.removeChild(anchor);
        }, 66);
				this.$message({
					type: 'success',
					message: '备份成功',
				})
			} catch (e) {
				this.$message({
					type: 'warning',
					message: e,
				})
				console.log(`backup`, { e: e })
			}
		},
		submitSuccess(response) {
			this.$message({ type: 'info', message: response.msg })
		},
	},
}
</script>
<style scoped >
.importFile {
	display: inline-block;
	margin-left: 10px;
}

::v-deep .el-form-item{
  margin: 0;
}
::v-deep .el-form-item__label{
  font-size: 18px;
}
</style>
